package com.sanqing.iframe;

import com.sanqing.dao.Dao;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyVetoException;

public class BorrowSettings extends JInternalFrame {
    JTextField maxNum = new JTextField(20);
    JTextField yajin = new JTextField(20);
    JTextField maxDays=new JTextField(20);
    JButton exitBut = new JButton("取消");

    public BorrowSettings() {
        super("图书借阅设置");
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        setFrameIcon(new ImageIcon("images/icon.png"));
        setLayout(new GridLayout(4, 2));
        setBounds(100, 100, 400, 200);
        Dao.showBorrowSettings(maxNum,yajin,maxDays);
        final JLabel maxNumLabel = new JLabel("最长借阅天数:");
        maxNumLabel.setFont(new Font("宋体", Font.PLAIN, 25));
        maxNum.setFont(new Font("宋体", Font.PLAIN, 25));
        getContentPane().add(maxNumLabel);
        getContentPane().add(maxNum);

        final JLabel yajinLabel = new JLabel("逾期每日扣除：");
        yajinLabel.setFont(new Font("宋体", Font.PLAIN, 25));
        yajin.setFont(new Font("宋体", Font.PLAIN, 25));
        getContentPane().add(yajinLabel);
        getContentPane().add(yajin);

        final JLabel maxDaysLabel=new JLabel("最大借书量:");
        maxDaysLabel.setFont(new Font("宋体", Font.PLAIN, 25));
        maxDays.setFont(new Font("宋体", Font.PLAIN, 25));
        getContentPane().add(maxDaysLabel);
        getContentPane().add(maxDays);
        final JButton submit = new JButton("确定");
        submit.setFont(new Font("宋体", Font.PLAIN, 25));
        exitBut.setFont(new Font("宋体", Font.PLAIN, 25));
        submit.addActionListener(new submitAction());
        exitBut.addActionListener(new exitButAction());
        getContentPane().add(submit);
        getContentPane().add(exitBut);
        validate();
    }

    private class submitAction implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (maxNum.getText().equals("")) {
                JOptionPane.showMessageDialog(BorrowSettings.this, "请输入最大借书天数");
                return;
            }
            if (yajin.getText().equals("")) {
                JOptionPane.showMessageDialog(BorrowSettings.this, "请输入逾期每日扣除押金数");
                return;
            }if (maxDays.getText().equals("")) {
                JOptionPane.showMessageDialog(BorrowSettings.this, "请输入最大借书数");
                return;
            }

            if (Dao.setBorrowSettings(maxNum.getText(), yajin.getText(),maxDays.getText())){
                JOptionPane.showMessageDialog(BorrowSettings.this, "修改成功");
                exitBut.doClick();
            } else {
                JOptionPane.showMessageDialog(BorrowSettings.this, "修改失败");
            }
        }
    }

    private class exitButAction implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                BorrowSettings.this.setClosed(true);
            } catch (PropertyVetoException propertyVetoException) {
                propertyVetoException.printStackTrace();
            }
        }
    }
}
